package LC;

/**
 * @ClassName LongestPalindrome
 * @Description: 最长回文串
 * @Author jiajun
 * @Date 2021/4/2 
 * @Version V1.0
 **/
public class LongestPalindrome {
    public static void main(String[] args) {
        System.out.println(longestPalindrome("cbbbc"));
    }
    public static String longestPalindrome(String s) {
        int l;
        int maxLen = 1;
        int start = 0;
        if ((l = s.length()) == 1) return s;
        boolean[][] dp = new boolean[l][l];
        for (int i = 0; i < l; i++) {
            dp[i][i] = true;
        }
        for (int j = 1; j < l; j++) {
            for (int i = 0; i < j; i++) {
                if (s.charAt(i) != s.charAt(j)) dp[i][j] = false;
                else {
                    if (j - i < 3) dp[i][j] = true;
                    else dp[i][j] = dp[i + 1][j - 1];
                }
                if (dp[i][j] && maxLen < j - i + 1) {
                    maxLen = j - i + 1;
                    start = i;
                }
            }
        }
        return s.substring(start, start + maxLen);
    }

}
